package com.mj.guoqi.activity;

import java.util.Timer;
import java.util.TimerTask;

import com.SYY.cbsdk.ui.AppControl;
import com.dyk.hfsdk.dao.util.DevListener;
import com.dyk.hfsdk.ui.Access;
import com.mj.guoqi.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
/**
 * 国旗查询
 * @author zhaominglei
 * @date 2015-8-15
 */
public class MainActivity extends Activity implements OnClickListener,DevListener {
	@SuppressWarnings("unused")
	private static final String TAG = MainActivity.class.getSimpleName();
	public static final String MUMAYI_APPID = "c5159bb53c6f47b03RoZjsbDqqyPHzyAyMGiQF4Z390ZSZLOr6qE4WuB71wJT/vc1A"; //app_id
	public static final String MUMAYI_APPCHANNEL = "木蚂蚁"; //app_channel
	private boolean isExit = false;
	private TimerTask timerTask;
	private LinearLayout miniAdLinearLayout; //迷你广告
	private EditText countrynameText; //国家名称输入框
	private String countryname; //国家名称
	private Button searchBtn; //查询
	private Button searchBtnAll; //查询全部
	private Intent intent;
	private Button appOffersButton; //推荐应用
	private Access access;
	private AppControl appControl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		init();
	}
	
	private void init() {
		miniAdLinearLayout = (LinearLayout)findViewById(R.id.miniAdLinearLayout);
		countrynameText = (EditText)findViewById(R.id.guoqi_countryname_edt);
		searchBtn = (Button)findViewById(R.id.guoqi_search_btn);
		searchBtnAll = (Button)findViewById(R.id.guoqi_searchall_btn);
		appOffersButton = (Button)findViewById(R.id.appOffersButton);
		
		searchBtn.setOnClickListener(this);
		searchBtnAll.setOnClickListener(this);
		appOffersButton.setOnClickListener(this);
		
		appControl = AppControl.getInstance();
		appControl.init(MainActivity.this, MUMAYI_APPID, MUMAYI_APPCHANNEL);
		appControl.loadPopAd(MainActivity.this);
		appControl.showPopAd(MainActivity.this, 60 * 1000);
		appControl.showInter(MainActivity.this, miniAdLinearLayout);
		
		access = Access.getInstance();
		// 初始化带player_id
		access.init(MainActivity.this, MUMAYI_APPID, MUMAYI_APPCHANNEL);
		// 设置初始积分                                                              
		access.setdefaultSCORE(this, 100);
		// 设置获取积分的监听
		access.setAppListener(this, this);
	}
	
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.guoqi_search_btn:
			countryname = countrynameText.getText().toString();
			if (countryname == null || countryname.equals("")) {
				Toast.makeText(getApplicationContext(), R.string.guoqi_countryname_hint, Toast.LENGTH_SHORT).show();
				return;
			}
			intent = new Intent(MainActivity.this, GuoqiListActivity.class);
			intent.putExtra("referer", "MainActivity_search");
			intent.putExtra("keyword", countryname);
			startActivity(intent);
			break;
			
		case R.id.guoqi_searchall_btn:
			intent = new Intent(MainActivity.this, GuoqiListActivity.class);
			intent.putExtra("referer", "MainActivity_searchall");
			startActivity(intent);
			break;

		case R.id.appOffersButton:
			access.openWALL(MainActivity.this);
			break;
			
		default:
			break;
		}
	}
	
	@Override
	public void onBackPressed() {
		if (isExit) {
			MainActivity.this.finish();
		} else {
			isExit = true;
			Toast.makeText(MainActivity.this, R.string.exit_msg, Toast.LENGTH_SHORT).show();
			timerTask = new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			};
			new Timer().schedule(timerTask, 2*1000);
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		appControl.close(this);
	}
	
	@Override
	public void onDevSucceed(int eggs) {
		System.out.println("积分获取成功:" + eggs);
	}

	@Override
	public void onDevFailed(String message) {
		System.out.println("积分获取失败:" + message);
	}

	@Override
	public void onClose(Context context) {
		((Activity) context).finish();
	}	
}
